package com.wechat.page.comps;

import com.swak.fx.support.AbstractComp;
import com.swak.fx.support.AbstractPage;
import com.swak.fx.support.FXMLView;
import com.swak.fx.support.ui.UI;
import com.swak.fx.support.ui.UIAction;
import com.swak.fx.support.ui.UIIcon;
import com.swak.fx.support.ui.UITextFlow;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.LongProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleLongProperty;
import javafx.fxml.FXML;
import javafx.geometry.Pos;
import javafx.geometry.VPos;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.text.Text;

/**
 * TitlePage.
 * 
 * @author 618lf
 */
@FXMLView(title = "聊天标题", value = "TitleComp.fxml", css = "TitleComp.css")
public class TitleComp extends AbstractComp implements TextComp {

	@FXML
	protected GridPane titlePane;
	@FXML
	protected UITextFlow title;
	@FXML
	protected Text number;
	@FXML
	protected Pane placeholder;
	@FXML
	protected HBox ctlBtns;
	@FXML
	protected HBox moreBtns;

	// 控制按钮
	protected UIAction topBtn;
	protected UIAction minBtn;
	protected UIAction maxBtn;
	protected UIAction closeBtn;

	// 更多的按钮
	protected UIAction moreBtn;

	public TitleComp(AbstractPage parent) {
		super(parent);
		this.topBtn = new UIAction();
		this.minBtn = new UIAction();
		this.maxBtn = new UIAction();
		this.closeBtn = new UIAction();
		this.moreBtn = new UIAction();
	}

	@Override
	public void initializeParts() {
		super.initializeParts();
		
		this.closeBtn.getStyleClass().add("close-btn");
		this.topBtn.getStyleClass().add("top-btn");
		
		this.topBtn.textNodeProperty().set(new UIIcon(UI.Icons.THUMB_TACK));
		this.minBtn.textNodeProperty().set(new UIIcon(UI.Icons.MINUS));
		this.maxBtn.textNodeProperty().set(new UIIcon(UI.Icons.SQUARE));
		this.closeBtn.textNodeProperty().set(new UIIcon(UI.Icons.REMOVE));
		this.ctlBtns.getChildren().addAll(this.topBtn, this.minBtn, this.maxBtn, this.closeBtn);

		this.moreBtn.textNodeProperty().set(new UIIcon(UI.Icons.ELLIPSIS_H));
		this.moreBtn.setVisible(false);
		this.moreBtns.getChildren().add(this.moreBtn);
		this.moreBtns.setAlignment(Pos.CENTER_RIGHT);

		this.title.setMaxWidth(200);
		GridPane.setFillHeight(title, false);
		GridPane.setHgrow(title, Priority.NEVER);
		GridPane.setHgrow(number, Priority.NEVER);
		GridPane.setVgrow(title, Priority.ALWAYS);
		GridPane.setVgrow(number, Priority.ALWAYS);
		GridPane.setValignment(title, VPos.CENTER);
		GridPane.setValignment(number, VPos.CENTER);
		GridPane.setValignment(title, VPos.CENTER);
	}

	@Override
	public void setupEventHandlers() {
		super.setupEventHandlers();
		this.registerUIEvent(this.titlePane, UI.Events.Window_Drag);
		this.registerUIEvent(this.moreBtns, UI.Events.Window_Drag);
		this.registerUIEvent(this.topBtn, UI.Events.Window_Fix);
		this.registerUIEvent(this.minBtn, UI.Events.Window_Min);
		this.registerUIEvent(this.maxBtn, UI.Events.Window_Max);
		this.registerUIEvent(this.closeBtn, UI.Events.Window_Close);
		this.registerUIEvent(this.titlePane, UI.Events.Window_Max2);
		this.registerUIEvent(this.placeholder, UI.Events.Window_Drag);
		this.registerUIEvent(this.placeholder, UI.Events.Window_Max2);
	}

	@Override
	public void setupBindings() {
		super.setupBindings();
	}

	@Override
	public void setupValueChangedListeners() {
		super.setupValueChangedListeners();
		this.fixActiveProperty().addListener((ob, o, n) -> {
			if (n) {
				this.topBtn.getStyleClass().add("active");
			} else {
				this.topBtn.getStyleClass().remove("active");
			}
		});
	}

	@Override
	public void windowStateChange(int state) {
		super.windowStateChange(state);
		this.fixActiveProperty().set((state & UI.States.Window_Fix.index) == UI.States.Window_Fix.index);
	}

	// ================================================================================
	// Properties
	// ================================================================================
	private BooleanProperty fixActiveProperty = new SimpleBooleanProperty(false);

	public BooleanProperty fixActiveProperty() {
		return fixActiveProperty;
	}

	private final LongProperty refreshProperty = new SimpleLongProperty();

	public LongProperty refreshProperty() {
		return refreshProperty;
	}
}